﻿using System;
using System.Windows.Forms;
using System.Windows.Threading;
using mmCore.Common;
using mmUI.Managers;
using mmUI.UiUtils;

namespace mmUI.ViewModels
{
    public class SettingsWindowVm: NotifyPropertyChangedBase
    {
        private Dispatcher _mDispatcher;

        public SettingsWindowVm(Dispatcher dispatcher)
        {
            _mDispatcher = dispatcher;
        }


        public bool UseGoogleAccount
        {
            set
            {
                Config.ProviderType = value ? ProviderType.GoogleAccount : ProviderType.XmlFile;
                RaisePropertyChanged(() => UseGoogleAccount);
                RaisePropertyChanged(() => PasswordEnabled);
            }
            get { return Config.ProviderType == ProviderType.GoogleAccount; }
        }

        public string GoogleUserId
        {
            get { return Config.GoogleLogin; }
            set
            {
                Config.GoogleLogin = value;
                RaisePropertyChanged(() => GoogleUserId);
            }
        }

        public bool PasswordEnabled
        {
            get { return UseGoogleAccount && RememberPassword; }
        }

        public bool RememberPassword
        {
            get { return Config.SavePassword; }
            set
            {
                Config.SavePassword = value;
                RaisePropertyChanged(() => RememberPassword);
                RaisePropertyChanged(() => PasswordEnabled);
            }
        }

        public string Password
        {
            get { return Config.Password; }
            set
            {
                Config.Password = value;
                RaisePropertyChanged(() => Password);
            }
        }

        public bool ShowUpdatesNotifications
        {
            get { return Config.ShowUpdatesNotifications; }
            set
            {
                Config.ShowUpdatesNotifications = value;
                RaisePropertyChanged(() => ShowUpdatesNotifications);
            }
        }

        public string JdownloaderLocation
        {
            set
            {
                Config.JdownloaderExe = value;
                RaisePropertyChanged(() => JdownloaderLocation);
            }
            get { return Config.JdownloaderExe; }
        }

        public string FilesTubeArgument
        {
            get { return Config.FilestubeArg; }
            set
            {
                Config.FilestubeArg = value;
                RaisePropertyChanged(() => FilesTubeArgument);
            }
        }

        public RelayCommand BrowseForJdownloader
        {
            get
            {
                return new RelayCommand((o) =>
                {
                    _mDispatcher.Invoke(new Action(() =>
                    {
                        var ofd = new OpenFileDialog {DefaultExt = "exe", CheckFileExists = true, Multiselect = false};
                        if (ofd.ShowDialog(App.Current.MainWindow.GetIWin32Window()) == DialogResult.OK)
                        {
                            JdownloaderLocation = ofd.FileName;
                        }
                    }));
                });
            }
        }
    }
}
